#!/bin/sh

# Start/stop/restart znc

# User account under which ZNC should run; only change this if you 
# know what you are doing :-)
ZNC_USER="@ZNC_USER@"

# First, a sanity check
if [[ ! -e /etc/znc/configs/znc.conf ]]; then
   echo "Warning: /etc/znc/configs/znc.conf does not exist."
   echo "You should create a config file by running the following command:"
   echo "  su - $ZNC_USER -c '/usr/bin/znc --makeconf --datadir=/etc/znc'"
   exit 1
fi

znc_start() {
   if [[ -e /var/lock/znc ]]; then
      echo "ZNC is already running, or there is a stale lock file at "
      echo "/var/lock/znc - if you are sure that znc is not already "
      echo "running, then remove the lock and try again."
      return 1
   else
      su - $ZNC_USER -c '/usr/bin/znc --datadir /etc/znc'
      touch /var/lock/znc
      return 0
   fi
}

znc_stop() {
   killall -SIGINT znc 2>/dev/null
   RETVAL=$?
   if (( "$RETVAL" != 0 )); then
      echo "ZNC does not appear to be running..."
   else
      rm -f /var/lock/znc
   fi
   return $RETVAL
}  

znc_restart() {
   znc_stop
   sleep 1
   znc_start
}

case "$1" in
   'start')
   znc_start
   ;;
   'stop')
   znc_stop
   ;;
   'restart')
   znc_restart
   ;;
   *)
   echo "Usage: $0 start|stop|restart"
   exit 1
esac

exit $?

